const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');

const multer=require('multer');
//设置上传路径
var multerObj=multer({dest:'./www/img'});
const fs=require('fs');
const pathLib=require('path')
//引用自定义
const commte=require('../../../libs/common.js');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});
//========================
//业务逻辑
module.exports = function() {
    var router = express.Router();

    router.use('/Adduerinfo',function(req,res,next){
        //讲密码进行加密
        var userPwd=commte.md5(req.body.userPwd+commte.MD5_SUFFIX);
        //用户呢称
        const s=commte.getdata();
        var Nickname="xyxy"+s.replace("-", "");
        var pwdpay="";
        var userAddr="";
        var userEmail="";

        if(req.files.length==0)
        {

            var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

            function generateMixed(n) {
                var res = "";
                for(var i = 0; i < n ; i ++) {
                    var id = Math.ceil(Math.random()*35);
                    res += chars[id];
                }
                return res;
            }
            // const date = new Date();
            // const year = date.getFullYear();
            // const month = date.getMonth()+1;
            // const day = date.getDate();
            // const hour = date.getHours();
            // const minute = date.getMinutes();
            // const second = date.getSeconds();


            db.query("INSERT INTO `userinfo`(`userId`, `Nickname`, `userImg`, `userName`, `userPwd`, `integral`, `balance`, `PayPwd`, `userAddr`, `userEmail`, `usertime`) VALUES (0,'"+Nickname +"','userimg.jpg','"+req.body.userName+"','"+userPwd+"',"+ +0+","+ +0+",'"+ pwdpay +"','"+userAddr+"','"+userEmail+"','"+commte.getdata()+"')",function (err,data) {
                if(err)
                {
                    console.log(err)
                    res.status(500).send('<script>alert("出错啦");</script>').end();
                }else
                {
                    res.status(400).send('<script>alert("添加成功");window.location.href="uerinfo.html";</script>').end();
                    next();
                }
            });

        }else {
            //console.log("原名文件"+req.files[0].originalname);
            //获得文件扩展名
            //console.log("文件后缀"+pathLib.parse(req.files[0].originalname).ext);
            //文件全名
            var ext = pathLib.parse(req.files[0].originalname).ext;
            //console.log("dxcvbnm" + ext);
            //文件旧存放路径
            var oldPath = req.files[0].path;
            // console.log("文件旧存放路径"+oldPath)
            //文件新存放路径
            var newPath = req.files[0].path + ext;
            // console.log("文件新存放路径" + newPath)
            //文件新名
            var newFileName = req.files[0].filename + ext;

            fs.rename(oldPath, newPath, function (err) {
                if (err) {

                    res.status(500).send('<script>alert("出错啦");</script>').end();
                }
                else {
                    var pwdpay="";
                    var userAddr="";
                    var userEmail="";
                    db.query("INSERT INTO `userinfo`(`userId`, `Nickname`, `userImg`, `userName`, `userPwd`, `integral`, `balance`, `PayPwd`, `userAddr`, `userEmail`, `usertime`) VALUES (0,'"+Nickname +"','"+newFileName+"','"+req.body.userName+"','"+userPwd+"',"+ +0+","+ +0+",'"+ pwdpay +"','"+userAddr+"','"+userEmail+"','"+commte.getdata()+"')",function (err,data) {
                        if(err)
                        {
                            res.status(500).send('<script>alert("出错啦");</script>').end();
                        }else
                        {
                            res.status(400).send('<script>alert("添加成功");window.location.href="uerinfo.html";</script>').end();
                            next();
                        }
                    });

                }
            });
        }

    });
    router.get('/Adduerinfo.html',function(req,res){
        res.render("admin/Adduerinfo.ejs",{activityinfo:req.activityid});
    });
    return router;
}